﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//
using System.Web.Services;
using DDSC.OracleADO;
using System.Collections;
using System.Text;
using System.Data;

public partial class krj_tracking_defect_bulletin : BasePermissionPage
{
    
    //搜尋SQL
    [WebMethod]
    public static Object GetTracking_Defect(string DISPATCH_DEPT, string CHECK_DATE, string txtRECEIVER, PageInfo p_PageInfo)
    {
        Object results = new Object();
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();

            m_sb.Append(" select A.F60505 CHECK_DATE,D.DEPT_NAME DISPATCH_DEPT,E.DEPT_NAME NOTICE_DEPT,F.DEPT_NAME RECEIVED_DEPT ");
            m_sb.Append(" ,I.p30104 DISPATCHER,A.F60521 || ' ' || H.F21002 DEFECT_REASON,A.F60510 ITEM_COUNT,A.F60507 SHIPPER,A.F60508 RECEIVER,A.F60510 ITEM_COUNT_ORG,A.F60501 || A.F60502 REF_ITEM_NO ");
            m_sb.Append(" from (select F60501,F60502,F60504,F60505,F60506,F60507,F60508,F60510,F60517,F60521,F605Z1 from AS400_ZDF605  ");
            //發送所站(畫面上必輸條件)
            m_sb.Append("        where F60505 >= :CHECK_DATE1 and F60506 =:DISPATCH_DEPT and  F60519 <> 'B' ");
            m_arrParam.Add(m_dba.GetOracleParam(":CHECK_DATE1", CHECK_DATE));
            m_arrParam.Add(m_dba.GetOracleParam(":DISPATCH_DEPT", DISPATCH_DEPT));
            
            //收貨人
            if (txtRECEIVER != "")
            {
                m_sb.Append(" AND F60508 like :txtRECEIVER ");
                m_arrParam.Add(m_dba.GetOracleParam(":txtRECEIVER", "%" + txtRECEIVER + "%"));
            }
            m_sb.Append("       ) A ");
            m_sb.Append(" inner join (select F63101,F63102,F631A1 from AS400_ZDF631 ");
            //點貨日期(畫面上必輸條件)
            m_sb.Append(" where F63151 >= :CHECK_DATE ");
            m_arrParam.Add(m_dba.GetOracleParam(":CHECK_DATE", CHECK_DATE));
            m_sb.Append(" ) B on A.F60501 = B.F63101 and A.F60502 = B.F63102 ");
            m_sb.Append(" left join (select F61971,F61904,F619Z1  from AS400_ZDF619) G on B.F631A1 = G.F619Z1 and ((A.F605Z1 <> B.F631A1 and G.F61904 = A.F60501) or (A.F605Z1 = B.F631A1 and G.F61904 is null)) ");
            m_sb.Append(" left join MIS_DEPT_AP D on A.F60506 = D.DEPT_ID ");
            m_sb.Append(" left join MIS_DEPT_AP E on A.F60504 = E.DEPT_ID ");
            m_sb.Append(" left join MIS_DEPT_AP F on A.F60517 = F.DEPT_ID ");
            m_sb.Append(" left join AS400_ZDF210 H on A.F60521 = H.F21001 ");
            m_sb.Append(" left join AS400_PNF301 I on G.F61971 = I.p30102 ");
            //m_sb.Append(" where ((A.F60501 || A.F60502 <> B.F631A1 and G.F61904 = A.F60501) or (A.F60501 || A.F60502 = B.F631A1 and G.F61904 is null)) ")
            
            results = ADOGridHandler(m_sb, m_arrParam, p_PageInfo);
            //20130628**********************************************
            //記錄最後查詢的條件
            krj_tracking_defect_bulletin sa = new krj_tracking_defect_bulletin();
            sa.savecondition( DISPATCH_DEPT,  CHECK_DATE,  txtRECEIVER);
        }
        catch (Exception ex)
        {
            BasePermissionPage BP = new BasePermissionPage();
            results = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_tracking_defect_bulletin.aspx.cs", "GetTracking_Defect", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return (results);

    }
    

    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = null;
        try
        {
            ds = DBINT();
            if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000")
            {
                ddlDISPATCH_DEPT.DataTextField = "DEPT_NAME";
                ddlDISPATCH_DEPT.DataValueField = "DEPT_ID";
                ddlDISPATCH_DEPT.DataSource = ds.Tables["DEPT"];
                ddlDISPATCH_DEPT.DataBind();
            }
        }
        catch (Exception ex)
        {
            lbl_message.Text = BP_Message;
            ERROR_DBSAVE("krj_tracking_defect_bulletin.aspx.cs", "Page_Load", ex.Message, "", "c#", "0", ex.StackTrace);
        }


    }
    
    protected DataSet DBINT()
    {
        DataSet ds = null;
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();

            m_sb.Append(" select A.bi4101 DEPT_ID ,A.bi4101 || '-' || B.DEPT_NAME DEPT_NAME ");
            m_sb.Append(" from as400_bif41 A left join MIS_DEPT_AP  B on A.BI4101 = B.DEPT_ID ");
            m_sb.Append(" where BI4121 = '1' and BI4122 = '2' ");
            m_sb.Append(" order by A.bi4101 ");

            ds = m_dba.runExecuteQuery("DEPT", m_sb.ToString(), m_arrParam);
            //if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() != "000")
            //{
            //    lbl_message.Text = "as400_bif41: 資料讀取錯誤！" + ds.Tables[0].Rows[0]["LogMessage"];
            //}

        }
        catch (Exception ex)
        {
            lbl_message.Text = BP_Message;
            ERROR_DBSAVE("krj_tracking_defect_bulletin.aspx.cs", "Page_Load", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return ds;
    }
    protected void btn_exist_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
    protected void savecondition(string DISPATCH_DEPT, string CHECK_DATE, string txtRECEIVER)
    {
        try
        {
            DropDownList ddl = new DropDownList();
            TextBox tx = new TextBox();

            var controlItem = new List<ControlItem>() 
            {
              new ControlItem("ddlDISPATCH_DEPT", DISPATCH_DEPT, ddl.GetType()),
              new ControlItem("txt_START_DATE", CHECK_DATE, tx.GetType()) ,
              new ControlItem("txtRECEIVER", txtRECEIVER, tx.GetType()) 
              
            };
            ConditionRecordUtil.RecordSearchConditionData(controlItem);
        }
        catch (Exception ex)
        {
            ERROR_DBSAVE("krj_tracking_defect_bulletin.aspx.cs", "savecondition", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }
}